package cdu.javaendwork.Controller.LoginAndRegister;

import cdu.javaendwork.model.sqlModel.User;
import cdu.javaendwork.service.Impl.Login.LoginServiceImpl;
import cdu.javaendwork.service.Impl.User.UserServiceImpl;
import cdu.javaendwork.service.Interface.Login.LoginService;
import cdu.javaendwork.service.Interface.User.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

@WebServlet("/login")
public class LoginController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求参数不为乱码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset = utf-8");
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        String inputCode = req.getParameter("inputCode");
        String sStatus = req.getParameter("status");
        int status = (sStatus == null || sStatus.equals("")) ? 1 : Integer.parseInt(sStatus);
        //验证验证码
        HttpSession session = req.getSession();
        //在会话中保存登录用户信息
        System.out.println(name);
        System.out.println("status: " + status);
        session.setAttribute("name", name);
        session.setAttribute("pwd", pwd);

        PrintWriter out = resp.getWriter();
        String validCode = (String) session.getAttribute("validCode");

        if (validCode == null || validCode.equals("") || !(validCode.toLowerCase(Locale.ROOT)).equals(inputCode.toLowerCase(Locale.ROOT))) {
            out.println("<script>alert('验证码有误！');window.location.href='/'</script>");
            return;
        }


        UserService userService=new UserServiceImpl();
        LoginService loginService=new LoginServiceImpl();
      //  User user = userService.(name, pwd);
        User user = null;
        //根据用户不同调用不同的登录
        if(status == 1) user = loginService.NormalUser(name,pwd);
        else user = loginService.AdminUser(name , pwd);

        System.out.println("loginService.AdminUser(name , pwd): " + loginService.AdminUser(name , pwd));
        System.out.println(user);


        if (user == null) {
            out.println("<script>alert('登录失败！');window.location.href='/'</script>");
        } else if (status == 1 && status == user.getStatus()) {
            session.setAttribute("user", user);
            out.println("<script>alert('普通用户登录成功！');window.location.href='"+req.getContextPath()+"/post_list'</script>");
        } else if (status == 2 && status == user.getStatus()) {
            session.setAttribute("user", user);
            out.println("<script>alert('管理员登录成功！');window.location.href='"+req.getContextPath()+"/admin/user_list'</script>");
        } else {
            out.println("<script>alert('登录失败！');window.location.href='/'</script>");
        }
    }
}
